package com.szqhyxj.youxiangju.biz.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.biz.zxing.ScanActivity;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class RegisterActivity extends BaseActivity implements RegisterContract.View{

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.verification_code)
    EditText verificationCode;
    @BindView(R.id.acquire)
    TextView acquire;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.ensure_password)
    EditText ensurePassword;
    @BindView(R.id.referral_code)
    EditText referralCode;
    @BindView(R.id.scan)
    ImageView scan;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.agree)
    CheckBox agree;
    @BindView(R.id.user_contract)
    TextView userContract;
    @BindView(R.id.register)
    ImageView register;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, RegisterActivity.class);
        activity.startActivity(intent);
    }

    private RegisterContract.Presenter presenter;

    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        ButterKnife.bind(this);
        presenter = new RegisterPresenter(this);
        initViews();
        initEvent();
    }

    private void initViews() {
        StringUtils.setEditTextHintSize(this, phone, "请输入手机号", 12);
        StringUtils.setEditTextHintSize(this, verificationCode, "请输入验证码", 12);
        StringUtils.setEditTextHintSize(this, password, "请输入密码（6-16位字母与数字组合）", 12);
        StringUtils.setEditTextHintSize(this, ensurePassword, "请确认密码", 12);
        StringUtils.setEditTextHintSize(this, referralCode, "请输入推荐码", 12);
    }

    private void initEvent() {
        agree.setChecked(true);
        presenter.setAgree(true);
        agree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                presenter.setAgree(isChecked);
            }
        });


        phone.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setAccount(s.toString());
            }
        });

        verificationCode.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setVerificationCode(s.toString());
            }
        });

        password.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setPassword(s.toString());
            }
        });

        ensurePassword.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setEnsurePassword(s.toString());
            }
        });

        referralCode.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setReferralCode(s.toString());
            }
        });
    }

    @Override
    public void showRegisterEnable() {
        register.setBackgroundResource(R.mipmap.login_btn_pre_zhuche);
        register.setEnabled(true);
    }

    @Override
    public void showRegisterDisable() {
        register.setBackgroundResource(R.mipmap.login_btn_zhuche);
        register.setEnabled(false);
    }

    @Override
    public void navToScan() {
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setPrompt("请将二维码置于取景框内扫描");
        //intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
        intentIntegrator.setCaptureActivity(ScanActivity.class);
        intentIntegrator.initiateScan();
    }

    // Get the results:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "取消扫码", Toast.LENGTH_LONG).show();
            } else {
                String resultstr = result.getContents();
                String str = resultstr.substring(0, resultstr.indexOf("username=") + 9);
                String userPhone = UrlParser.parse(resultstr, "userPhone");
                referralCode.setText(userPhone);
                presenter.setReferralCode(userPhone);
               // Toast.makeText(this, " " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void navToUserContract() {
        String path = String.format("userAgreement");
        WebActivity.startToH5(this, "用户协议", path);
    }

    @Override
    public void showAcquiringVerificationCode(int number) {
        acquire.setText(String.format("%ss", String.valueOf(number)));
        acquire.setEnabled(false);
    }

    @Override
    public void showAcquiredVerificationCode() {
        acquire.setText("获取");
        acquire.setEnabled(true);
    }

    @Override
    public void navBack() {
        finish();
    }

    @Override
    public void showAcquiredVerificationCodeSuccess() {
        Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showAcquiredVerificationCodeFail(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showRegisterSuccess() {
        MerchantContainerActivity.start(this);
    }

    @Override
    public void showRegisterFail(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.acquire, R.id.scan, R.id.back, R.id.user_contract, R.id.register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.acquire:
                presenter.acquireVerificationCode();
                break;
            case R.id.scan:
                presenter.scan();
                break;
            case R.id.back:
                presenter.back();
                break;
            case R.id.user_contract:
                presenter.userContract();
                break;
            case R.id.register:
                presenter.register();
                break;
        }
    }


}